//
//  SelectionView.m
//  礼物
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 liangsicheng. All rights reserved.
//

#import "SelectionView.h"
#import "CycleModel.h"
#import <UIImageView+WebCache.h>
#import "Model.h"
#import "ImageViewModel.h"
#import "Model.h"
#import "SelectionCell.h"
@implementation SelectionView



-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}
-(void)createView
{
    
    self.myTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64,WIDTH, HEIGHT-64) style:UITableViewStylePlain];
    self.myTableView.delegate=self;
    self.myTableView.dataSource=self;
    [self addSubview:self.myTableView];
    
    
    
    self.lunboScrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT/5)];
    self.lunboScrollview.pagingEnabled = YES;
    self.lunboScrollview.contentOffset=CGPointMake(WIDTH, 0);
    UIView *aView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT/3)];
    [aView addSubview:self.lunboScrollview];
    self.myTableView.tableHeaderView=aView;
    aView.backgroundColor=[UIColor colorWithRed:0.000 green:1.000 blue:0.000 alpha:0.460];
    self.myTableView.tableHeaderView.userInteractionEnabled=YES;
    aView.userInteractionEnabled=YES;
    self.lunboScrollview.userInteractionEnabled=YES;
    self.lunboScrollview.delegate=self;
    self.pagecontrol=[[UIPageControl alloc]initWithFrame:CGRectMake(WIDTH/3, HEIGHT/6, 100, 20)];
    self.pagecontrol.currentPageIndicatorTintColor=[UIColor whiteColor];
    self.imageView1=[[UIImageView alloc]initWithFrame:CGRectMake(WIDTH/8, self.lunboScrollview.frame.origin.y+self.lunboScrollview.frame.size.height, WIDTH/4, aView.frame.size.height-self.lunboScrollview.frame.size.height)];
        UITapGestureRecognizer *tap1=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction1:)];
    UITapGestureRecognizer *tap2=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction2:)];
    
    [aView addSubview:self.imageView1];
    self.imageView2=[[UIImageView alloc]initWithFrame:CGRectMake(WIDTH/2+WIDTH/8, self.lunboScrollview.frame.origin.y+self.lunboScrollview.frame.size.height, WIDTH/4, aView.frame.size.height-self.lunboScrollview.frame.size.height)];
    [aView addSubview:self.imageView2];
    [self.imageView1 addGestureRecognizer:tap1];
    [self.imageView2 addGestureRecognizer:tap2];
    self.imageView1.userInteractionEnabled=YES;
    self.imageView2.userInteractionEnabled=YES;

    [aView addSubview: self.pagecontrol];
}
-(void)tapAction1:(UIGestureRecognizer *)tap
{
    [self.delegate tapActionOnePushDetailController];
}
-(void)tapAction2:(UIGestureRecognizer *)tap
{
    [self.delegate tapActionTwoPushDetailController];

}
-(void)setImageV:(NSMutableArray *)array
{
    ImageViewModel *model1=[array objectAtIndex:0];
    ImageViewModel *model2=[array objectAtIndex:1];
    [self.imageView1 sd_setImageWithURL:[NSURL URLWithString:model1.icon_url]];
    [self.imageView2 sd_setImageWithURL:[NSURL URLWithString:model2.icon_url]];
    

}
-(void)setLunboPicture
{
    self.pagecontrol.numberOfPages=self.lunboArray.count;

    id imageFirst = [self.lunboArray firstObject];
    id imageLast = [self.lunboArray lastObject];
    [self.lunboArray insertObject:imageLast atIndex:0];
    [self.lunboArray addObject:imageFirst];

    for (int index = 0; index < self.lunboArray.count; index++)
    {
        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.lunboScrollview.frame.origin.x + self.lunboScrollview.frame.size.width * index, 0, self.lunboScrollview.frame.size.width, self.lunboScrollview.frame.size.height)];
        imageView.tag=index;
         CycleModel *model=[self.lunboArray objectAtIndex:index];
        [imageView sd_setImageWithURL:[NSURL URLWithString:model.image_url]];
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(TapAction:)];
        [imageView addGestureRecognizer:tap];
        imageView.userInteractionEnabled = YES;
        [self.lunboScrollview addSubview:imageView];
        
    }
    self.timer=[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
    self.index=1;
}
-(void)TapAction:(UITapGestureRecognizer *)tap
{
    UIImageView *imageV=(UIImageView *)tap.view;
    CycleModel *model=[self.lunboArray objectAtIndex:imageV.tag];
    [self.delegate cyclePushDetailController:model];
}

-(void)timerAction:(NSTimer *)timer
{
    [UIView animateWithDuration:0.5 animations:^{
        [self.lunboScrollview setContentOffset:CGPointMake((self.index + 1) * self.lunboScrollview.frame.size.width, 0)];
        
        
    }];
    self.pagecontrol.currentPage = (self.lunboScrollview.contentOffset.x - self.lunboScrollview.frame.size.width) / (WIDTH);
    self.index++;
    //如果滑动到最后一页
    if (self.lunboScrollview.contentOffset.x == (self.lunboArray.count-1)*self.lunboScrollview.frame.size.width) {
        //快速移动到第一页
        [self.lunboScrollview setContentOffset:CGPointMake(self.lunboScrollview.frame.size.width, 0)];
        
        self.index = 1;
        
    }
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.firstPageCellArray.count;
    
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    Model *model=[self.firstPageCellArray objectAtIndex:section];
    return model.short_title;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150*HEIGHT/667;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indefitier=@"reuse";
    SelectionCell *cell=[tableView dequeueReusableCellWithIdentifier:indefitier];
    if (cell==nil) {
        cell=[[SelectionCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:indefitier];
    }
    Model *model=[self.firstPageCellArray objectAtIndex:indexPath.section];
    [cell setModel:model];
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    Model *model=[self.firstPageCellArray objectAtIndex:indexPath.section];
    [self.delegate selectionViewPushDetailController:model];
    
}




//让UITableView的section header view不悬停的方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat sectionHeaderHeight =40*HEIGHT/667;
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    }
    else if (scrollView.contentOffset.y>=sectionHeaderHeight)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.lunboScrollview.contentSize = CGSizeMake(WIDTH * self.lunboArray.count, 0);
    
    if (self.lunboScrollview==scrollView){
        self.pagecontrol.currentPage = (self.lunboScrollview.contentOffset.x - self.lunboScrollview.frame.size.width) / WIDTH;
        if (self.lunboScrollview.contentOffset.x >= self.lunboScrollview.frame.size.width * (self.lunboArray.count - 1))
        {
            //            NSLog(@"最后一页");//循环滚动
            [self.lunboScrollview setContentOffset:CGPointMake(self.lunboScrollview.frame.size.width, 0) animated:NO];
        }
        if (self.lunboScrollview.contentOffset.x <= 0) {
            [self.lunboScrollview setContentOffset:CGPointMake(self.lunboScrollview.frame.size.width * (self.lunboArray.count - 2), 0) animated:NO];
        }
    }
    
    

        
    

    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
